package com.htandroid.reconstitute.utils;

import android.content.Context;
import android.widget.Toast;


/**
 * Created by john on 2016/6/16.
 */
public class ToastUtil
        extends Toast {
    private static Toast mToast = null;
    private static Context sContext;
    private static String  oldMsg;
    private static long oneTime = 0;
    private static long twoTime = 0;


    public ToastUtil(Context context) {
        super(context);
    }

    public static void showToast(Context context, String content) {
        showToast(content);
//        mToast = ToastUtil.makeText(context, R.layout.view_toast, content, Toast.LENGTH_SHORT);
//        mToast.show();
    }


    public static void init(Context context) {
        sContext = context;
    }

    // 不重复弹出
    public static void showToast(String s) {
        if (s == null) {
            s = "is null!";
        }
        if (mToast == null) {
            mToast = Toast.makeText(sContext, s, Toast.LENGTH_SHORT);
            mToast.show();
            oneTime = System.currentTimeMillis();
        } else {
            twoTime = System.currentTimeMillis();
            if (s.equals(oldMsg)) {
                if (twoTime - oneTime > Toast.LENGTH_SHORT) {
                    mToast.show();
                }
            } else {
                oldMsg = s;
                mToast.setText(s);
                mToast.show();
            }
            oneTime = twoTime;
        }
    }


}
